//
//  SevenDetailCell.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/7.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SevenDetailCell: UICollectionViewCell {
    
    var _bgView:UIView!;
    var _nextView:UIView!;
    var _data:[String:AnyObject]?;
    
    public var _isShowNextView:Bool = false;
    fileprivate var _bgLb:UILabel?;
    
    override init(frame: CGRect) {
        super.init(frame:frame);
        
        self.onCreateUIForView();
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //TODO:UI
    fileprivate func onCreateUIForView() {
        _bgView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height));
        _bgView.backgroundColor = .white;//UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1.0);
        self.contentView.addSubview(_bgView);
        _bgView.layer.shadowColor = UIColor.lightGray.cgColor;
        _bgView.layer.shadowRadius = 10;
        _bgView.layer.shadowOffset = CGSize(width: 0, height: 0);
        _bgView.layer.shadowOpacity = 1.0;
        self.onCreateUIForBgView();
        
        
        _nextView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height));
        _nextView.backgroundColor = UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1.0);
//        _nextView.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 1, 0);
        self.contentView.insertSubview(_nextView, belowSubview: _bgView);
        self.onCreateUIForNextView();
    }
    
    fileprivate func onCreateUIForBgView() {
        let lb = UILabel(frame: _bgView.bounds);
        lb.text = "第一面描述";
        lb.textColor = .lightGray;
        lb.textAlignment = .center;
        _bgLb = lb;
        _bgView.addSubview(lb);
    }
    
    fileprivate func onCreateUIForNextView() {
        let lb = UILabel(frame: _bgView.bounds);
        lb.text = "第二面描述";
        lb.textColor = .lightGray;
        lb.textAlignment = .center;
        _nextView.addSubview(lb);
    }
    
    //TODO:Actions
    public func onChangeContentView(isNext:Bool) {
        let animation = CATransition();
        animation.duration = 2;
        animation.type = .fade;
        animation.subtype = .fromRight;
        self.contentView.layer.add(animation, forKey: "");
        self.contentView.exchangeSubview(at: 0, withSubviewAt: 1);
        
//        let animation = CABasicAnimation(keyPath: "transform.rotation.y");
//        animation.duration = 2;
//        animation.fromValue = NSValue(caTransform3D: CATransform3DMakeRotation(0, 0, 1, 0));
//        animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat(Float.pi/2), 0, 1, 0));
//        self.contentView.layer.add(animation, forKey: "");
////        CATransaction.setDisableActions(true);
//        UIView.animate(withDuration: 2.0) {
////            CATransaction.setDisableActions(false);
//            self.contentView.exchangeSubview(at: 0, withSubviewAt: 1);
//        }
        
//        if isNext {
//            UIView.animate(withDuration: 2, animations: {
//                self.contentView.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 1, 0);
//                self._bgView.removeFromSuperview()
//            }) { (isFinish) in
//                self.contentView.addSubview(self._nextView);
//            }
//
//        } else {
//            UIView.animate(withDuration: 2, animations: {
//                self.contentView.layer.transform = CATransform3DIdentity;
//                self._nextView.removeFromSuperview();
//            }) { (isFinish) in
//                self.contentView.addSubview(self._bgView);
//
//            }
//        }
//        _isShowNextView = isNext;
        
    }
    
    func onChangeUIByData(data:[String:AnyObject]) {
        _data = data;
        self.setNeedsLayout();
    }
    
    override func layoutSubviews() {
        super.layoutSubviews();
        
        if _data != nil {
            _bgLb?.text = (_data!["title"] as! String);
        }
    }
}
